﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_careerupdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = Int32.Parse(Request.QueryString["cid"].ToString());
            }
            catch {
                Response.Redirect("admin_career.aspx");
            }
            if (!IsPostBack) {
                BindData(id);
            }
            imgPicUrl.ImageUrl = hfPicUrl.Value;
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "ckfinder/";
            _FileBrowser.SetupCKEditor(txbContent);

        }

        public void BindData(int id)
        {
            BindNotice();
            BindaCareer(id);
        }
        private void BindaCareer(int id)
        {
            CareerInfo career = CareerInfoLogic.GetOneCareerInfo(id);
            txbCName.Text = career.CareerCName;
            txbContent.Text = career.CareerContent;
            txbEName.Text = career.CareerEName;
            txbPublishTime.Text = career.PublishTime.ToShortDateString();
            hfPicUrl.Value = career.PicURL;

        }
        private void BindNotice()
        {
            noticesuccess.Visible = false;
            noticefail.Visible = false;
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "initCloseNotice();initImgFromHidden();", true);
        }

        private CareerInfo CreateOneModel()
        {
            CareerInfo career = new CareerInfo();
            career.CareerCName = txbCName.Text.Trim();
            career.CareerEName = txbEName.Text.Trim();
            career.CareerContent = txbContent.Text.Trim();
            career.PicURL = hfPicUrl.Value;
            try
            {
                career.PublishTime = System.Convert.ToDateTime(txbPublishTime.Text);
            }
            catch
            {
                career.PublishTime = DateTime.Now;
            }
            return career;
        }

        private int InitFromUrl(string url, int first)
        {
            int rtn = first;
            try
            {
                rtn = Int32.Parse(Request.QueryString[url].ToString());
            }
            catch { }
            return rtn;
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = Int32.Parse(Request.QueryString["cid"].ToString());
            }
            catch {
                Response.Redirect("admin_career.aspx");
            }

            CareerInfo career = CreateOneModel();
            career.CareerID = id;
            int isSuccess = CareerInfoLogic.UpdateCareerInfo(career);
            if (isSuccess == 1)
            {
                ChangeStatus(true);
            }
            else
            {
                ChangeStatus(false);
            }
        }
    }
}
